package com.example.smartplay.controller;


import com.example.smartplay.entity.Admin;
import com.example.smartplay.entity.Result;
import com.example.smartplay.entity.Student;
import com.example.smartplay.entity.User;
import com.example.smartplay.mapper.AdminMapper;
import com.example.smartplay.service.AdminService;
import com.example.smartplay.utils.JwtUtil;
import com.example.smartplay.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;
    //登录
    @PostMapping("/login")
    public Result login(String username, String password) {
        //查询用户是否存在
        Admin admin = adminMapper.findByUsername(username);
        if (admin == null) {
            return Result.error("用户名或密码错误");
        } else {
            if (Md5Util.getMD5String(password).equals(admin.getPassword())) {
                //登录成功
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", admin.getId());
                claims.put("username", admin.getUsername());
                claims.put("role", admin.getRole());

                String token = JwtUtil.genToken(claims);
                return Result.success(token);

            } else {
                return Result.error("用户名或密码错误");
            }
        }
    }
}
